<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="内容分类" prop="type">
        <!-- <el-select v-model="queryParams.type" placeholder="批次类型" clearable style="width: 240px">
            <el-option v-for="dict in dict.type.sys_video_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select> -->
          <el-select v-model="queryParams.type" placeholder="请选择内容分类" @change="changeTvType">
            <el-option v-for="item in tvTypeOptions" :key="item.id" :label="item.name" :value="item.id" :disabled="item.status == 1" ></el-option>
            </el-select>
      </el-form-item>
      <!-- <el-form-item label="内容类型" prop="typeData"> -->
        <!-- <el-select v-model="queryParams.type" placeholder="批次类型" clearable style="width: 240px">
            <el-option v-for="dict in dict.type.sys_video_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select> -->
          <!-- <el-select v-model="queryParams.typeData" placeholder="请选择内容类型详情">
            <el-option v-for="item in tvDataOptions" :key="item.id" :label="item.name" :value="item.id" :disabled="item.status == 1"></el-option>
            </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="操作人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="开始时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-hasPermi="['monitor:batch:remove']"
        >清空</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="batchList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="内容分类" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_video_type" :value="scope.row.type"/>
        </template> 
      </el-table-column>
      <el-table-column label="个数" align="center" prop="number" />
      <el-table-column label="内容开始编号" align="center" prop="contentStart" />
      <el-table-column label="内容结束编号" align="center" prop="contentEnd" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template> 
      </el-table-column>
      <el-table-column label="详情" align="center" prop="remake" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remake" /> -->
      <!-- <el-table-column label="操作人" align="center" prop="userName" /> -->
       <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:batch:remove']"
          >删除</el-button>
        </template>
      </el-table-column> 
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改批次管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="内容开始ID" prop="contentStart">
          <el-input v-model="form.contentStart" placeholder="请输入内容开始ID" />
        </el-form-item>
        <el-form-item label="内容结束ID" prop="contentEnd">
          <el-input v-model="form.contentEnd" placeholder="请输入内容结束ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remake">
          <el-input v-model="form.remake" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="操作人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入操作人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBatch, getBatch, delBatch, addBatch, updateBatch,cleanBatch } from "@/api/system/batch";
import { getContentType,getContentTypeData } from '@/api/index';

export default {
  name: "Batch",
  dicts: ['sys_video_type','sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内容分类选项
      tvTypeOptions: [],
      // 内容分类的类型详情选项
      tvDataOptions: [],
      // 批次管理表格数据
      batchList: [],
       // 日期范围
       dateRange: [],      
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        typeData: null,
        startTime: null,
        endTime: null,
        contentStart: null,
        contentEnd: null,
        status: null,
        remake: null,
        userName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getContentType();
    this.getList();
  },
  methods: {
    /** 查询批次管理列表 */
    getList() {
      this.loading = true;
      // listBatch(this.queryParams).then(response => {
      listBatch(this.addDateRange(this.queryParams, this.dateRange)).then(response => {              
        this.batchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleClean() {
      this.$modal.confirm('是否确认清空所有批量记录数据项？').then(function() {
        return cleanBatch();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("清空成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        startTime: null,
        endTime: null,
        contentStart: null,
        contentEnd: null,
        status: null,
        remake: null,
        userName: null
      };
      this.resetForm("form");
    },
    changeTvType(data){
      this.plotType = null;
      this.tvDataOptions = null;
      this.getContentTypeData({
          "type": data,
        });
    },
    getContentType(){
      getContentType().then(response =>{
        this.tvTypeOptions = response.data;
        if(this.queryParams.type !== null){
          this.getContentTypeData({
          "type": this.queryParams.type,
         });
        }
     
      }).catch(error => {
        console.log("获取内容分类的响应:"+error);
        return
      });

    },

    getContentTypeData(data){
      getContentTypeData(data).then(response =>{
        this.tvDataOptions = response.data;
        }).catch(error => {
          console.log("获取内容分类的类型详情响应:"+error);
          return
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.tvDataOptions = null;
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加批次管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBatch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改批次管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除批次管理编号为"' + ids + '"的数据项？').then(function() {
        return delBatch(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/batch/export', {
        ...this.queryParams
      }, `batch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
